<?php

namespace app\apiadmin\model\order;

use app\common\utils\Res;
use think\facade\Db;
use think\Model;

use app\apiadmin\service\statics\LiveUserGetDataService;

class OrderModel extends Model
{

    protected $name = "siyu_order";

    protected $pk = "id";

    /**
     * @author: zwx
     * @Description: 分页查询数据列表
     * @param {*} $map  查询条件
     * @param {*} $offset  分页偏移量
     * @param {*} $limit  每页数量
     * @Date: 2024-05-12 14:53:13
     */
    public static function pageMap($map, $findName, $offset, $limit,$param=[])
    {
        if(isset($param['signin_status']) && in_array($param['signin_status'],[0,2]) && isset($param['liveId']) && !empty($param['liveId'])){
            $data = self::alias('o')
            ->field($findName)
            ->join('xh_siyu_course c ', ' o.courseid = c.id')
            // ->leftJoin('xh_siyu_user u ', ' o.wx_openid = u.openid')
            ->leftJoin('xh_siyu_sys_member m ', ' m.uid = o.tutorId')
            ->where($map)
            ->where('o.wx_unionid','in',function($query) use($param){
                $where=[
                    ['del','=',0],
                    ['chengjiao_status','=',0],
                    ['signin_status','=',$param['signin_status']],
                    ['liveId','in',$param['liveId']]
                ];
                $query->table('xh_siyu_invitation_user')->where($where)->field('wx_unionid');
            })
            ->limit($offset, $limit)
            ->group('o.id')
            ->order('o.create_time desc')
            ->select()
            ->each(function($item){
                $item['nickname']='';
                $item['wx_avatarurl']='';
                $user_info = Db::name('siyu_user')->where(['openid'=>$item['wx_openid']])->find();
                if($user_info){
                    $item['nickname']=$user_info['nickname'];
                    $item['wx_avatarurl']=$user_info['wx_avatarurl'];
                }
            });
        }else{
            $data = self::alias('o')
            ->field($findName)
            ->join('xh_siyu_course c ', ' o.courseid = c.id')
            // ->leftJoin('xh_siyu_user u ', ' o.wx_openid = u.openid')
            ->leftJoin('xh_siyu_sys_member m ', ' m.uid = o.tutorId')
            ->where($map)
            ->limit($offset, $limit)
            ->group('o.id')
            ->order('o.create_time desc')
            ->select()
            ->each(function($item){
                $item['nickname']='';
                $item['wx_avatarurl']='';
                $user_info = Db::name('siyu_user')->where(['openid'=>$item['wx_openid']])->find();
                if($user_info){
                    $item['nickname']=$user_info['nickname'];
                    $item['wx_avatarurl']=$user_info['wx_avatarurl'];
                }
            });


        }
        
        return $data;
    }

    /**
     * @author: crg
     * @Description: app订单列表
     * @param {*} $map  查询条件
     * @param {*} $offset  分页偏移量
     * @param {*} $limit  每页数量
     * @Date: 2024-05-12 14:53:13
     */

    public static function getMyOrder($map, $findName, $offset, $limit)
    {

        $data = self::alias('o')
            ->field($findName)
            ->join('xh_siyu_course c ', ' o.courseid = c.id')
            ->where($map)->limit($offset, $limit)->order('o.create_time desc')->select();
        return $data;
    }

    /**
     * @author: zwx
     * @Description: 查询数据总数
     * @param {*} $map 查询条件
     * @Date: 2024-05-12 14:53:43
     */
    public static function pageMapCount($map,$param='')
    {
        if(isset($param['signin_status']) && in_array($param['signin_status'],[0,2]) && isset($param['liveId']) && !empty($param['liveId'])){
            return self::alias('o')
            ->join('xh_siyu_course c ', ' o.courseid = c.id')
            // ->leftJoin('xh_siyu_user u ', ' o.wx_openid = u.openid')
            ->leftJoin('xh_siyu_sys_member m ', ' m.uid = o.tutorId')
            ->where('o.wx_unionid','in',function($query) use($param){
                $where=[
                    ['del','=',0],
                    ['chengjiao_status','=',0],
                    ['signin_status','=',$param['signin_status']],
                    ['liveId','in',$param['liveId']]
                ];
                $query->table('xh_siyu_invitation_user')->where($where)->field('wx_unionid');
            })
            ->where($map)->group('o.id')->count();
        }else{
            return self::alias('o')
            ->join('xh_siyu_course c ', ' o.courseid = c.id')
            // ->leftJoin('xh_siyu_user u ', ' o.wx_openid = u.openid')
            ->leftJoin('xh_siyu_sys_member m ', ' m.uid = o.tutorId')
            ->where($map)->group('o.id')->count();
        }
    }

    /**
     * @author: crg
     * @Description: 订单详情
     * @param {*} $map  查询条件
     * @Date: 2024-05-12 14:53:13
     */
    public static function infoMap($map, $findName)
    {

        $data = self::alias('o')
            ->field($findName)
            ->join('xh_siyu_course c ', ' o.courseid = c.id')
            ->where($map)->order('o.create_time desc')->select();
        return $data;
    }

    /**
     * @author: zwx
     * @Description: 创建退款订单
     * @param {*} $map
     * @Date: 2024-05-25 11:41:12
     */
    public static function createSelledOrder($map)
    {

        try {
            $data = Db::name('siyu_order_selled')->insert($map);
            return $data;
        } catch (\Exception $e) {
            return Res::error($e->getMessage());
        }

    }

    /**
     * @author: zwx
     * @Description: 更新订单
     * @Date: 2024-05-25 12:06:23
     */
    public static function updateOrderStatus($param)
    {

        try {
            $update_data=['part_type'=>$param['part_type']];
            if(isset($param['state']) && !empty($param['state'])){
                $update_data['state']=$param['state'];
            }
            if(isset($param['refund_time']) && !empty($param['refund_time'])){
                $update_data['refund_time']=$param['refund_time'];
            }
            
            $data = Db::name('siyu_order')->where(['id' => $param['orderid']])->save($update_data);

            //更新行为记录添加数据详情
            $collection = 'live_order';
            $map = [];
            $map['liveId'] = $param['liveId'];
            $map['order_num'] = $param['order_num'];

            $update = ['$set' => ['state'=>3]];
            $liveOrder = new LiveUserGetDataService($collection);
            $liveOrder->updateOrder($map,$update);
            return $data;
        } catch (\Exception $e) {
            return Res::error($e->getMessage());
        }

    }

}
